class Ws {

    constructor() {
        this.socket;
        this.on;
    }

    open(url) {
        this.socket = new WebSocket(url);
        this.socket.onopen = function () {
            console.log('连接打开');
        };
        this.socket.onclose = function () {
            console.log('连接关闭');
        };
        this.socket.addEventListener('message', (res) => {
            let data = JSON.parse(res.data);
            if (this.on) {
                this.on(data);
            }
        });
    }

    onMessage(on) {
        this.on = on;
    }
}

const ws = new Ws();

export default ws;